﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using TerrainProcessor;

namespace ScorchedTanks
{
    class SceneMenu : Scene
    {
        SpriteBatch spriteBatch;
        SpriteFont myFont;
        ContentManager Content; 
        List<string> log = new List<string>();
        int selectedSessionIndex = 0;
        AvailableNetworkSessionCollection activeSessions;
        Networking globalNetworkObjet;
        GraphicsDeviceManager graphics;

        public SceneMenu(GraphicsDeviceManager g, ContentManager content, SpriteBatch sb, SpriteFont sf, Networking n)
        {
            myFont = sf;
            spriteBatch = sb;
            globalNetworkObjet = n;
            Content = content;
            graphics = g;
            //
        }

        public override void update(float dTime)
        {

            base.update(dTime);
        }

        public override void draw(GraphicsDeviceManager g)
        {
            spriteBatch.Begin();
            if (SignedInGamer.SignedInGamers.Count < 1)
            {
                  if (!Guide.IsVisible)
                    Guide.ShowSignIn(1, false);
                  log.Add("Opened user signing interface");
            }
            else
            {
                if (activeSessions==null)
                    activeSessions = NetworkSession.Find(NetworkSessionType.SystemLink, 4, null);

                if (activeSessions.Count == 0)
                {
                    spriteBatch.DrawString(myFont, "No active sessions found \nPress C to create a new one or s to Search Again\n", new Vector2(101, 101), Color.Black);
                    spriteBatch.DrawString(myFont, "No active sessions found \nPress C to create a new one or s to Search Again\n", new Vector2(100, 100), Color.White);
                }
                else
                {
                    float y = 100;
                    spriteBatch.DrawString(myFont, "Available sessions (J=join, B=back, C=create)", new Vector2(101, y + 1), Color.Black);
                    spriteBatch.DrawString(myFont, "Available sessions (J=join, B=back, C=create)", new Vector2(100, y), Color.White);

                    y += myFont.LineSpacing * 2;

                    for (int sessionIndex = 0; sessionIndex < activeSessions.Count; sessionIndex++)
                    {
                        Color color = Color.Black;

                        if (sessionIndex == selectedSessionIndex)
                            color = Color.Yellow;

                        spriteBatch.DrawString(myFont, activeSessions[sessionIndex].HostGamertag, new Vector2(100, y), color);
                        y += myFont.LineSpacing;
                    }

                }

            }
            
            spriteBatch.End();

            base.draw(g);
        }

        public override void handleInput(InputManager Input)
        {
            if (Input.isNewKeyPress(Keys.C))
            {
                globalNetworkObjet.CreateSession();
                SceneManager.scenesToAdd.Add(new SceneLobby(graphics, Content, spriteBatch, myFont, globalNetworkObjet));
                readyToExit = true;
            }
            else if (Input.isNewKeyPress(Keys.S))
            {
                activeSessions = NetworkSession.Find(NetworkSessionType.SystemLink, 4, null);
            }
            else if (Input.isNewKeyPress(Keys.J))
            {
                AvailableNetworkSession sessionToJoin = activeSessions[selectedSessionIndex];
                globalNetworkObjet.networkSession = NetworkSession.Join(sessionToJoin);
                SceneManager.scenesToAdd.Add(new SceneLobby(graphics, Content, spriteBatch, myFont, globalNetworkObjet));
                readyToExit = true;            
            }
            else if (Input.isNewKeyPress(Keys.Up))
            {
                // Select the previous session from the list.
                if (selectedSessionIndex > 0)
                    selectedSessionIndex--;
            }
            else if (Input.isNewKeyPress(Keys.Down))
            {
                // Select the next session from the list.
                if (selectedSessionIndex < activeSessions.Count - 1)
                    selectedSessionIndex++;
            }
            else if (Input.isNewKeyPress(Keys.Escape))
            {
                //Exit
                readyToExit = true;
            }

            base.handleInput(Input);
        }


    }
}
